package charts.series.bars; 

import flash.display.Sprite;
import flash.geom.Point;

class ECandle extends ElementBase {
	
	var high:Float;
	var low:Float;

	
	public function new( index:Float, style:Dynamic, group:Float ) {
		
		super(index, style, style.colour, style.tip, style.alpha, group);
	}
	
	//
	// a candle chart has many values used to display each point
	//
	function parse_value( value:Dynamic ):Void {
		
		// set top (open) and bottom (close)
		super.parse_value( value );
		
		if( ! ( Std.is( value, Number) ) )
		{
			this.high = value.high;
			this.low = value.low;
		}
	}
	
	function replace_magic_values( t:String ): String {
		
		t = super.replace_magic_values( t );
		t =  StringTools.replace(t,'#high#', NumberUtils.formatNumber( this.high ));
		t =  StringTools.replace(t,'#open#', NumberUtils.formatNumber( this.top ));
		t =  StringTools.replace(t,'#close#', NumberUtils.formatNumber( this.bottom ));
		t =  StringTools.replace(t,'#low#', NumberUtils.formatNumber( this.low ));
		
		return t;
	}
	
	public override function resize( sc:ScreenCoordsBase ):Void {
		
		var s:ScreenCoords = cast( sc, ScreenCoords);
		var tmp:Dynamic = s.get_bar_coords( this.index, this.group );

		// 
		var bar_high:Int		= sc.get_y_from_val(this.high, this.right_axis);
		var bar_top:Int		= sc.get_y_from_val(this.top, this.right_axis);
		var bar_bottom:Int	= sc.get_y_from_val(this.bottom, this.right_axis);
		var bar_low:Int		= sc.get_y_from_val(this.low, this.right_axis);
		
		var top:Float;
		var height:Float;
		var upside_down:Bool = false;
		
		if( bar_bottom < bar_top ) {
			upside_down = true;
		}
		
		height = Math.abs( bar_bottom - bar_top );
		
		//
		// move the Sprite to the correct screen location:
		//
		this.y = bar_high;
		this.x = tmp.x;
		
		//
		// tell the tooltip where to show its self
		//
		this.tip_pos = new flash.geom.Point( this.x + (tmp.width / 2), this.y );
		
		var mid:Int = tmp.width / 2;
		this.graphics.clear();
		// top line
		this.graphics.beginFill( this.colour, 1.0 );
		this.graphics.moveTo( mid-1, 0 );
		this.graphics.lineTo( mid+1, 0 );
		this.graphics.lineTo( mid+1, bar_top-bar_high );
		this.graphics.lineTo( mid-1, bar_top-bar_high );
		this.graphics.endFill();
		
		// box
		this.graphics.beginFill( this.colour, 1.0 );
		this.graphics.moveTo( 0, bar_top-bar_high );
		this.graphics.lineTo( tmp.width, bar_top-bar_high );
		this.graphics.lineTo( tmp.width, bar_bottom-bar_high );
		this.graphics.lineTo( 0, bar_bottom-bar_high );
		this.graphics.endFill();
		
		// top line
		this.graphics.beginFill( this.colour, 1.0 );
		this.graphics.moveTo( mid-1, bar_bottom-bar_high );
		this.graphics.lineTo( mid+1, bar_bottom-bar_high );
		this.graphics.lineTo( mid+1, bar_low-bar_high );
		this.graphics.lineTo( mid-1, bar_low-bar_high );
		this.graphics.endFill();
		
	}
		
}
